﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace TreeBuilder
{
	public class EllipseAdorner : Adorner
	{
		public EllipseAdorner(UIElement adornedElement) : base(adornedElement)
		{}

		public Brush EllipseBrush
		{
			get { return (Brush)GetValue(EllipseBrushProperty); }
			set { SetValue(EllipseBrushProperty, value); }
		}

		public static readonly DependencyProperty EllipseBrushProperty =
			DependencyProperty.Register("EllipseBrush", typeof(Brush), typeof(EllipseAdorner), new PropertyMetadata(Brushes.LightGreen));

		public Pen EllipsePen
		{
			get { return (Pen)GetValue(EllipsePenProperty); }
			set { SetValue(EllipsePenProperty, value); }
		}

		public static readonly DependencyProperty EllipsePenProperty =
			DependencyProperty.Register("EllipsePen", typeof(Pen), typeof(EllipseAdorner), new PropertyMetadata(new Pen(Brushes.LightGreen, 1)));



		public double Size
		{
			get { return (double)GetValue(SizeProperty); }
			set { SetValue(SizeProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty SizeProperty =
			DependencyProperty.Register("Size", typeof(double), typeof(EllipseAdorner), new PropertyMetadata(4.0));



		public bool IsActive
		{
			get { return (bool)GetValue(IsActiveProperty); }
			set { SetValue(IsActiveProperty, value); }
		}

		// Using a DependencyProperty as the backing store for IsActive.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty IsActiveProperty =
			DependencyProperty.Register("IsActive", typeof(bool), typeof(EllipseAdorner), new PropertyMetadata(false));

        protected bool IsRendered { get; set; }

        protected void DrawPoints(IEnumerable<Point> points, DrawingContext drawingContext)
        {
            foreach (var point in points)
            {
                drawingContext.DrawEllipse(EllipseBrush, EllipsePen, point, Size, Size);
            }
        }

		protected override void OnRender(DrawingContext drawingContext)
		{
		    IsRendered = false;
			base.OnRender(drawingContext);
			if (!IsActive) return;

			var rect = new Rect(AdornedElement.DesiredSize);

		    var points = new Point[]
		        {
		            rect.TopLeft,
		            rect.TopRight,
		            rect.BottomLeft,
		            rect.BottomRight
		        };

		    DrawPoints(points, drawingContext);
		}
	}
}
